CSCS8010 ML Foundations Practical Lab-2¶

Importing Libraries¶

In [1]:
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
In [2]:
import plotly
plotly.offline.init_notebook_mode()

Plotting using the matplotlib.pyplot¶

In [3]:
fig, ax1 = plt.subplots(figsize=(5, 2.7), layout = "constrained")

# Defining x and y data points

x =[4, 8, 16, 2, 3, 9, 18, 6, 5, 15, 10, 8, 6]
 
y =[64, 72, 96, 81, 88, 99, 108, 92, 86, 100, 69, 77, 80]

# Plotting the scatter plot between x and y

ax1.scatter(x, y, c = 'red', s = 100, alpha = 0.6)

# Setting the labels for x-axis and y-axis

ax1.set_xlabel("x-axis")

ax1.set_ylabel("y-axis")

ax1.set_title("Scatter Plot between x and y")

# enabling the grid view

ax1.grid(True)

We can observe from the scatter plot that, in the beginning, as the x value increase, y values dropped. However, after crossing x = 14, y values are getting higher as the x increases.¶

Plotting using Plotly¶

In [4]:
# Importing the tips data

df = px.data.tips()
In [5]:
# Plotting the scatter plot for different weekdays between total_bill and tips

fig= px.scatter(df, x="total_bill", y = "tip", color="sex", size="size", facet_col="day")
fig.show()

The above plots illustrate that, during saturdays, the highest billed orders and tips obtained. Male visitors are comparitively higher on all days. Huge number of orders were billed less than USD 40 and the obtained tips would be mostly less than USD 8.¶

Plotting with Seaborn¶

In [6]:
# Importing the mpg data

cars = sns.load_dataset("mpg")
In [7]:
# Plotting the boxplot for mpg for different origins

sns.boxplot(x="origin", y="mpg", data=cars)
Out[7]:
<Axes: xlabel='origin', ylabel='mpg'>

The above box plot indicates that, the Japanese cars have higher average mpg than European and American cars. European cars have less variability in their average mpg.

In [8]:
# Plotting the line plot between acceleartion and mpg for different regions
sns.lineplot(data = cars, x = "acceleration", y = "mpg", hue = "origin")
Out[8]:
<Axes: xlabel='acceleration', ylabel='mpg'>

The accelearion and mpg have positive correlation. Japanese cars have higher mpg for standard acceleration. As per line plot, european cars have higher accelearion.